package com.example.hrsystem.ui.user;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.example.hrsystem.R;
import com.example.hrsystem.data.model.User;

/**
 * 个人中心界面Activity
 * 显示用户的个人信息和提供相关操作
 */
public class ProfileActivity extends AppCompatActivity {

    private UserViewModel userViewModel;
    private TextView nameTextView;
    private TextView employeeIdTextView;
    private TextView departmentTextView;
    private TextView positionTextView;
    private TextView emailTextView;
    private TextView phoneNumberTextView;
    private TextView entryDateTextView;
    private TextView userTypeTextView;
    private TextView statusTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        // 初始化UI组件
        initUI();

        // 初始化ViewModel
        userViewModel = new ViewModelProvider(this).get(UserViewModel.class);

        // 观察加载状态
        userViewModel.getIsLoading().observe(this, isLoading -> {
            // 可以在这里显示加载指示器
        });

        // 观察错误信息
        userViewModel.getErrorMessage().observe(this, errorMessage -> {
            if (errorMessage != null && !errorMessage.isEmpty()) {
                Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
            }
        });

        // 观察当前用户信息
        userViewModel.getCurrentUser().observe(this, this::updateUserProfile);

        // 加载当前用户信息（这里使用模拟数据，实际应用中应该从登录状态获取用户ID）
        // 注意：在实际应用中，应该从安全存储中获取当前登录用户的ID
        int currentUserId = 1; // 模拟用户ID
        userViewModel.loadCurrentUser(currentUserId);

        // 为了演示，添加一个模拟用户到数据库
        addMockUser();

        // 设置返回按钮点击事件
        findViewById(R.id.back_button).setOnClickListener(v -> finish());
    }

    /**
     * 初始化UI组件
     */
    private void initUI() {
        nameTextView = findViewById(R.id.user_name_text_view);
        employeeIdTextView = findViewById(R.id.employee_id_text_view);
        departmentTextView = findViewById(R.id.department_text_view);
        positionTextView = findViewById(R.id.position_text_view);
        emailTextView = findViewById(R.id.email_text_view);
        phoneNumberTextView = findViewById(R.id.phone_number_text_view);
        entryDateTextView = findViewById(R.id.entry_date_text_view);
        userTypeTextView = findViewById(R.id.user_type_text_view);
        statusTextView = findViewById(R.id.status_text_view);
    }

    /**
     * 更新用户个人信息显示
     */
    private void updateUserProfile(User user) {
        if (user != null) {
            nameTextView.setText(user.getName());
            employeeIdTextView.setText(user.getEmployeeId());
            departmentTextView.setText(user.getDepartment());
            positionTextView.setText(user.getPosition());
            emailTextView.setText(user.getEmail());
            phoneNumberTextView.setText(user.getPhoneNumber());
            entryDateTextView.setText(user.getEntryDate());
            userTypeTextView.setText(user.getUserType());
            statusTextView.setText(user.isActive() ? "在职" : "离职");
        }
    }

    /**
     * 添加模拟用户数据（仅用于演示）
     */
    private void addMockUser() {
        // 检查是否已存在模拟用户
        if (!userViewModel.getIsLoading().getValue()) {
            User mockUser = new User();
            mockUser.setEmployeeId("EMP1001");
            mockUser.setName("张三");
            mockUser.setDepartment("技术部");
            mockUser.setPosition("Android开发工程师");
            mockUser.setEmail("zhangsan@example.com");
            mockUser.setPhoneNumber("13800138000");
            mockUser.setEntryDate("2023-01-15");
            mockUser.setUserType("普通员工");
            mockUser.setActive(true);

            userViewModel.addUser(mockUser);
        }
    }
}